/**
 * @Probject Name: component-base
 * @Path: com.eg.ftbl.common.component.utilsCglibBeanUtils.java
 * @Create By 111008320
 * @Create In 2019年12月3日 上午8:36:09
 */
package com.util;

import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

public class BeanExtUtil extends BeanUtils{

    public static <T> List<T> copyList(List sourceList, Class<T> targetCls) {
        Assert.notNull(sourceList, "Source must not be null");
        T t;
        List<T> copyList = new ArrayList<>();
        try {
            for (Object source : sourceList) {
                t = targetCls.newInstance();
                BeanUtils.copyProperties(source, t);
                copyList.add(t);
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return copyList;
    }

    public static <T> T copyTo(Object sourceObject, Class<T> targetCls) {
        Assert.notNull(sourceObject, "Source must not be null");
        T t = null;
        try {
                t = targetCls.newInstance();
                BeanUtils.copyProperties(sourceObject, t);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }
}
